<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Design a 1/f spectrum shaping (pink-noise) filter</title>
<link rel="canonical" href="/Users/mcgrant/Projects/CVX/examples/filter_design/html/one_over_f_filter.html">
<link rel="stylesheet" href="../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Design a 1/f spectrum shaping (pink-noise) filter</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% "Filter design" lecture notes (EE364) by S. Boyd</span>
<span class="comment">% "FIR filter design via spectral factorization and convex optimization"</span>
<span class="comment">% by S.-P. Wu, S. Boyd, and L. Vandenberghe</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% Designs a log-Chebychev filter magnitude design given as:</span>
<span class="comment">%</span>
<span class="comment">%   minimize   max| log|H(w)| - log D(w) |   for w in [0,pi]</span>
<span class="comment">%</span>
<span class="comment">% where variables are impulse response coefficients h, and data</span>
<span class="comment">% is the desired frequency response magnitude D(w).</span>
<span class="comment">%</span>
<span class="comment">% We can express and solve the log-Chebychev problem above as</span>
<span class="comment">%</span>
<span class="comment">%   minimize   max( R(w)/D(w)^2, D(w)^2/R(w) )</span>
<span class="comment">%       s.t.   R(w) = |H(w)|^2   for w in [0,pi]</span>
<span class="comment">%</span>
<span class="comment">% where we now use the auto-correlation coeffients r as variables.</span>
<span class="comment">%</span>
<span class="comment">% As an example we consider the 1/sqrt(w) spectrum shaping filter</span>
<span class="comment">% (the so-called pink-noise filter) where D(w) = 1/sqrt(w).</span>
<span class="comment">% Here we use a logarithmically sampled freq range w = [0.01*pi,pi].</span>
<span class="comment">%</span>
<span class="comment">% Written for CVX by Almir Mutapcic 02/02/06</span>

<span class="comment">% parameters</span>
n = 40;      <span class="comment">% filter order</span>
m = 15*n;    <span class="comment">% frequency discretization (rule-of-thumb)</span>

<span class="comment">% log-space frequency specification</span>
wa = 0.01*pi; wb = pi;
wl = logspace(log10(wa),log10(wb),m)';

<span class="comment">% desired frequency response (pink-noise filter)</span>
D = 1./sqrt(wl);

<span class="comment">% matrix of cosines to compute the power spectrum</span>
Al = [ones(m,1) 2*cos(kron(wl,[1:n-1]))];

<span class="comment">% solve the problem using cvx</span>
cvx_begin
  variable <span class="string">r(n,1)</span>   <span class="comment">% auto-correlation coefficients</span>
  variable <span class="string">R(m,1)</span>   <span class="comment">% power spectrum</span>

  <span class="comment">% log-chebychev minimax design</span>
  minimize( max( max( [R./(D.^2)  (D.^2).*inv_pos(R)]' ) ) )
  subject <span class="string">to</span>
     <span class="comment">% power spectrum constraint</span>
     R == Al*r;
cvx_end

<span class="comment">% check if problem was successfully solved</span>
disp([<span class="string">'Problem is '</span> cvx_status])
<span class="keyword">if</span> ~strfind(cvx_status,<span class="string">'Solved'</span>)
  <span class="keyword">return</span>
<span class="keyword">end</span>

<span class="comment">% spectral factorization</span>
h = spectral_fact(r);

<span class="comment">% figures</span>
figure(1)
H = exp(-j*kron(wl,[0:n-1]))*h;
loglog(wl,abs(H),wl,D,<span class="string">'r--'</span>)
set(gca,<span class="string">'XLim'</span>,[wa pi])
xlabel(<span class="string">'freq w'</span>)
ylabel(<span class="string">'mag H(w) and D(w)'</span>)
legend(<span class="string">'optimized'</span>,<span class="string">'desired'</span>)
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling SDPT3 4.0: 4200 variables, 1841 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints = 1841
 dim. of sdp    var  = 1200,   num. of sdp  blk  = 600
 dim. of linear var  = 1800
 dim. of free   var  = 600 *** convert ublk to lblk
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|1.4e+04|1.9e+02|1.1e+07| 0.000000e+00  0.000000e+00| 0:0:00| spchol  1  1 
 1|0.908|0.977|1.3e+03|4.5e+00|3.5e+05|-6.779434e+02 -8.898649e+01| 0:0:00| spchol  1  1 
 2|0.933|0.963|8.7e+01|1.9e-01|2.1e+04|-3.493165e+01 -9.285894e+01| 0:0:00| spchol  1  1 
 3|0.986|0.997|1.2e+00|3.5e-03|3.8e+02|-1.025150e+00 -9.212412e+01| 0:0:00| spchol  2  2 
 4|0.966|0.471|4.1e-02|2.0e-03|6.4e+01|-3.813882e-01 -7.807639e+01| 0:0:00| spchol  2  2 
 5|0.490|0.167|2.1e-02|1.0e-02|6.4e+01|-3.414174e-01 -6.791929e+01| 0:0:01| spchol  2  2 
 6|0.657|0.889|7.4e-03|5.3e-03|1.2e+01|-5.162183e-01 -1.214292e+01| 0:0:01| spchol  2  2 
 7|1.000|0.914|9.2e-06|1.9e-03|1.5e+00|-8.557488e-01 -2.263174e+00| 0:0:01| spchol  2  2 
 8|1.000|0.714|5.3e-06|5.5e-04|5.6e-01|-9.800865e-01 -1.526533e+00| 0:0:01| spchol  2  2 
 9|1.000|0.172|3.1e-06|5.1e-04|4.7e-01|-1.022150e+00 -1.473351e+00| 0:0:01| spchol  2  2 
10|1.000|0.481|1.1e-06|2.6e-04|2.9e-01|-1.063172e+00 -1.348412e+00| 0:0:01| spchol  2  2 
11|1.000|0.327|2.7e-07|1.4e-04|1.9e-01|-1.111436e+00 -1.299974e+00| 0:0:01| spchol  2  2 
12|1.000|0.900|2.5e-07|2.6e-05|6.4e-02|-1.140627e+00 -1.202063e+00| 0:0:01| spchol  2  2 
13|0.768|0.913|8.3e-08|7.6e-06|3.4e-02|-1.160497e+00 -1.193737e+00| 0:0:01| spchol  2  2 
14|1.000|0.945|9.3e-09|3.9e-06|1.1e-02|-1.178238e+00 -1.188675e+00| 0:0:02| spchol  2  2 
15|0.951|0.840|5.3e-09|1.3e-06|2.1e-03|-1.185499e+00 -1.187516e+00| 0:0:02| spchol  2  2 
16|0.822|0.854|2.3e-09|2.4e-07|5.2e-04|-1.186859e+00 -1.187373e+00| 0:0:02| spchol  2  2 
17|0.929|0.894|6.4e-10|6.0e-08|5.9e-05|-1.187278e+00 -1.187336e+00| 0:0:02| spchol  2  2 
18|0.979|0.969|5.3e-11|6.9e-09|2.8e-06|-1.187329e+00 -1.187331e+00| 0:0:02| spchol  3  3 
19|0.993|0.988|7.0e-11|3.3e-10|4.9e-08|-1.187331e+00 -1.187331e+00| 0:0:02|
  stop: max(relative gap, infeasibilities) &lt; 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 19
 primal objective value = -1.18733104e+00
 dual   objective value = -1.18733109e+00
 gap := trace(XZ)       = 4.87e-08
 relative gap           = 1.44e-08
 actual relative gap    = 1.43e-08
 rel. primal infeas (scaled problem)   = 6.98e-11
 rel. dual     "        "       "      = 3.32e-10
 rel. primal infeas (unscaled problem) = 0.00e+00
 rel. dual     "        "       "      = 0.00e+00
 norm(X), norm(y), norm(Z) = 1.2e+01, 2.2e+02, 2.7e+02
 norm(A), norm(b), norm(C) = 5.0e+02, 2.0e+00, 3.6e+01
 Total CPU time (secs)  = 2.16  
 CPU time per iteration = 0.11  
 termination code       =  0
 DIMACS: 7.0e-11  0.0e+00  5.9e-09  0.0e+00  1.4e-08  1.4e-08
-------------------------------------------------------------------
 
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +1.18733
 
Problem is Solved
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="one_over_f_filter__01.png" alt=""> 
</div>
</div>
</body>
</html>